package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
class MyThreadPool{
    //创建一个工作队列---消息队列
    BlockingQueue<Runnable> workqueue;
    //往队列里塞任务的方法
    public void submit(Runnable task) throws InterruptedException {
        workqueue.put(task);
    }
    private int n;//线程池的线程数量
    public MyThreadPool(int n) {
        this.n = n;
        this.workqueue=new ArrayBlockingQueue<>(1000);
        //通过循环的方式创建出多条线程
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
                try {
                    Runnable task=workqueue.take();//取任务
                    task.run();//执行任务
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();//开启线程
        }
    }
}
public class demo30 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool=new MyThreadPool(10);
            for (int i=0;i<100;i++){
                int id=i;//任务编号
                //通过main线程向线程池提交任务
                myThreadPool.submit(()->{
                    System.out.println(Thread.currentThread().getName()+" "+"id="+id);
                });
            }
    }


}
